package com.five.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.five.exception.BusinessException;
import com.five.jwt.JwtUtils;
import com.five.jwt.Payload;
import com.five.jwt.RsaUtils;
import com.five.pojo.LoginInfo;
import com.five.pojo.ReadHistory;
import com.five.result.Result;
import com.five.service.IReadHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.security.PublicKey;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reader/readHistory")
public class ReadHistoryController {
    @Autowired
    private IReadHistoryService readHistoryService;
    @PostMapping("/add")
    public Result<ReadHistory> addReadHistory(@RequestParam Long novelId,@RequestHeader("token") String token) {
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(URLDecoder.decode(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile(), "UTF-8"));
            Payload<Map> payload = JwtUtils.getInfoFromToken(token, publicKey, Map.class);
            Object loginInfo = payload.getUserInfo().get("loginInfo");
            //序列化
            LoginInfo logininfo = JSON.toJavaObject((JSONObject) loginInfo, LoginInfo.class);
            Long readId = logininfo.getId();
            readHistoryService.addReadHistory(readId,novelId);
            return Result.success("添加阅读历史成功");
        }catch (BusinessException e){
            return Result.error(e.getMessage());
        }catch ( Exception  e) {
            e.printStackTrace();
            return Result.error("添加阅读历史失败");
        }
    }
    @PostMapping("/delete")
    public Result<ReadHistory> deleteReadHistory(@RequestBody List<Long> novelIds,@RequestHeader("token") String token) {
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(URLDecoder.decode(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile(), "UTF-8"));
            Payload<Map> payload = JwtUtils.getInfoFromToken(token, publicKey, Map.class);
            Object loginInfo = payload.getUserInfo().get("loginInfo");
            //序列化
            LoginInfo logininfo = JSON.toJavaObject((JSONObject) loginInfo, LoginInfo.class);
            Long readId = logininfo.getId();
            readHistoryService.deleteReadHistory(readId,novelIds);
            return Result.success("删除阅读历史成功");
        } catch (BusinessException e){
            return Result.error(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统异常，删除失败！");
        }
    }
    @PostMapping("/query")
    public Result<List<ReadHistory>> queryReadHistory(@RequestHeader("token") String token) {
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(URLDecoder.decode(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile(), "UTF-8"));
            Payload<Map> payload = JwtUtils.getInfoFromToken(token, publicKey, Map.class);
            Object loginInfo = payload.getUserInfo().get("loginInfo");
            //序列化
            LoginInfo logininfo = JSON.toJavaObject((JSONObject) loginInfo, LoginInfo.class);
            Long readId = logininfo.getId();
            List<ReadHistory> readHistories = readHistoryService.queryReadHistory(readId);
            return Result.success(readHistories);
        }catch (BusinessException e){
            return Result.error(e.getMessage());
        }catch (Exception e) {
           return Result.error("查询失败");
        }
    }
}